package concordion.plugin.maven;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Resource;
import org.apache.maven.project.MavenProject;
import org.apache.maven.reporting.AbstractMavenReport;
import org.apache.maven.reporting.MavenReportException;
import org.apache.maven.shared.filtering.MavenFilteringException;
import org.apache.maven.shared.filtering.MavenResourcesExecution;
import org.apache.maven.shared.filtering.MavenResourcesFiltering;
import org.codehaus.doxia.sink.Sink;
import org.codehaus.doxia.site.renderer.SiteRenderer;

/**
 * Goal which touches a timestamp file.
 *
 * @goal summary-report
 * @author bangroot
 * @phase site
 */
public class ConcordionSummaryReport extends AbstractMavenReport {

    /**
     * The Maven project
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    private MavenProject project;

    /**
     * @parameter expression="${session}"
     * @readonly
     * @required
     */
    private MavenSession session;
    
    /**
     * Directory where reports will go.
     * @parameter expression="${project.reporting.outputDirectory}"
     * @required
     * @readonly
     */
    private String outputDirectory;

    /**
     * SiteRenderer for maven project
     *
     * @component
     */
    private SiteRenderer siteRenderer;

    /**
     * Directory of the concordion test output results.
     * 
     * @parameter default-value="/tmp/concordion"
     */
    private String concordionOutputDirectory;

    /**
     * Define whether to copy the actual Concordion HTML results files to the site
     * and link them with the summary results.
     *
     * @parameter default-value=true
     */
    private boolean includeConcordionResults;

/**
     *
     * @component role="org.apache.maven.shared.filtering.MavenResourcesFiltering" role-hint="default"
     * @required
     */
    protected MavenResourcesFiltering mavenResourcesFiltering;

    protected SiteRenderer getSiteRenderer() {
        return siteRenderer;
    }

    protected String getOutputDirectory() {
        return outputDirectory;
    }

    protected MavenProject getProject() {
        return project;
    }

    public String getOutputName() {
        return "concordion";
    }

    public String getName(Locale locale) {
        return getBundle(locale).getString("concordion.summary.name");
    }

    public String getDescription(Locale locale) {
        return getBundle(locale).getString("concordion.summary.description");
    }

    private ResourceBundle getBundle(Locale locale) {
        return ResourceBundle.getBundle("concordion-summary", locale, this.getClass().getClassLoader());
    }

    protected void executeReport(Locale locale) throws MavenReportException {
        Sink sink = getSink();
        sink.section1();
        sink.sectionTitle1();
        sink.text(getBundle(locale).getString("concordion.summary.title"));
        sink.sectionTitle1_();
        File concordionDir = new File(concordionOutputDirectory);
        ConcordionReports reportCollection = new ConcordionReports(concordionDir);
        reportCollection.generateReport(sink, getBundle(locale), includeConcordionResults);
        sink.section1_();
        if (includeConcordionResults) {
            try {
                List resources = new ArrayList();
                Resource resource = new Resource();
                resource.setDirectory(concordionOutputDirectory);
                resources.add(resource);
                List fileFilters = new ArrayList();
                List nonFilteredExtensions = new ArrayList();
                nonFilteredExtensions.add("html");
                MavenResourcesExecution execution = new MavenResourcesExecution(resources, new File(getOutputDirectory() + "/concordion"), project, "UTF-8", fileFilters, nonFilteredExtensions, session);
                mavenResourcesFiltering.filterResources(execution);
            } catch (MavenFilteringException ex) {
                Logger.getLogger(ConcordionSummaryReport.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
